#include "TString.h"
#include "catch.hpp"
#include <fstream>
#include <iostream>
#include <jsoncpp/json/json.h>

/// Boundary test for the `JsonCpp` library.
/// See [here](https://linux.tips/programming/how-to-install-and-use-json-cpp-library-on-ubuntu-linux-os) for how to install the library on Ubuntu. See also:
/// * [Repository on GitHub](https://github.com/open-source-parsers/jsoncpp)
/// * [Doxygen pages for JsonCpp](https://open-source-parsers.github.io/jsoncpp-docs/doxygen/index.html)
/// Compile with `g++` using:
/// ```bash
/// g++ JsonReader.C -o JsonReader -ljsoncpp && ./JsonReader
/// ```

TEST_CASE("JsonReader", "[single-file]")
{
  std::ifstream ifs("tests/data/JsonReader.json");
  Json::Reader  reader;
  Json::Value   root;
  reader.parse(ifs, root, false);

  SECTION("Read values")
  {
    REQUIRE(root["first name"].asString().compare("Jon") == 0);
    REQUIRE(root["last name"].asString().compare("Snow") == 0);
    REQUIRE(root["integer"].asInt() == 123456);
    REQUIRE(root["float"].asFloat() == 0.55f);
  }

  SECTION("Read array of objects")
  {
    const Json::Value& val = root["batters"]["batter"];
    REQUIRE(root["batters"].isObject() == true);
    REQUIRE(val.isArray() == true);
    int i = 1001;
    for(auto& obj : val)
    {
      REQUIRE(obj["id"].asString().compare(Form("%d", i)) == 0);
      ++i;
    }
  }

  SECTION("Read array of different types")
  {
    const Json::Value& val = root["testArray"];
    REQUIRE(val[0].asString().compare("one") == 0);
    REQUIRE(val[1].asInt() == 2);
    REQUIRE(val[2].asFloat() == 5.5f);
  }
}